package com.blb.servlet.user;

import com.blb.dao.user.UserDao;
import com.blb.pojo.User;
import com.blb.service.user.impl.UserServiceImpl;
import com.blb.servlet.BaseServlet;
import com.blb.utils.ApplicationContextUtil;
import com.blb.utils.CodeUtils;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;

@WebServlet(value = "/user/*",loadOnStartup = 1)
public class UserServlet extends BaseServlet {

    private UserServiceImpl userService = (UserServiceImpl) ApplicationContextUtil.getBean(UserServiceImpl.class);

    public void register(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        try {
            User user = getEntity(User.class);
            String againPass = req.getParameter("againPass");
            String codeText = req.getParameter("codeText");
            // 验证密码
            if (!againPass.equals(user.getPass())){
                throw new Exception("两次输入的密码不相同");
            }
            //判断验证码
            if (CodeUtils.randomString.equals(codeText)){
                throw new Exception("验证码输入有误");
            }
            // 判断登录
            userService.register(user);
            resp.sendRedirect("/user/view/login");
        } catch (Exception e) {
            req.setAttribute("msg","注册失败:"+e.getMessage());
            forward("/user/view/register");
        }
    }

    public void login(HttpServletRequest req, HttpServletResponse resp) {
        try {
            User user = getEntity(User.class);
            String codeText = req.getParameter("codeText");
            //判断验证码
            String randomString = CodeUtils.randomString.substring(0,4);
            if (!randomString.equals(codeText)){
                throw new Exception("验证码输入有误");
            }
            //登录密码验证
            userService.login(user);
            //七天免登录
            HttpSession session = req.getSession();
            //判断是否勾选七天免登录
            String limit = req.getParameter("limit");
            String name = req.getParameter("name");
            if("on".equals(limit)){
                //勾选了
                Cookie cookie = new Cookie("JSESSIONID", session.getId());
                cookie.setMaxAge(604800);
                resp.addCookie(cookie);
            }else {
                //没勾选
                Cookie cookie = new Cookie("JSESSIONID", session.getId());
                cookie.setMaxAge(-1);
                resp.addCookie(cookie);
            }
            User user1 = userService.getByName(name);
            session.setAttribute("user", user1);
            resp.sendRedirect("/user/index/view/index");
        }  catch (Exception e) {
            try {
                req.setAttribute("msg","登录失败:"+e.getMessage());
                forward("/user/index/index");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }


    public void code(HttpServletRequest req, HttpServletResponse resp) {
        try {
            BufferedImage bufferedImage = CodeUtils.newCode();
            resp.setContentType("image/jpeg");
            ImageIO.write(bufferedImage, "jpg",resp.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(1111);
        }
    }

    public void outLogin(HttpServletRequest req, HttpServletResponse resp){
        try {
            forward("/user/view/login");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
